home *** CD-ROM | disk | FTP | other *** search
- #include "MacIncludes.h"
- #include "Rk.h"
- #include "Structs.h"
- #include "Strings.h"
-
- extern WindowPtr gText;
- extern void SetCheckBox(dialog,item,state);
- extern Str255 gFileName;
-
-
- void DoSave();
-
- extern char *pStrcat(char *s,char *t);
- extern char *pStrcpy(char *s,char *t);
- extern char *PathNameFromWD(long vRefNum, char *s);
- extern void EnableDItem(DialogPtr dialog, short item, short state);
- extern short GetRealRefNum(long vRefNum);
-
- Point gLocation = {0x40,0x40};
- SFReply reply;
- SFTypeList typeList={kFileType};
-
- Boolean gOpenPrime=true;
- Boolean gOpenClear=false;
- Boolean gNewFile=true;
- Boolean gNone=false;
- extern Boolean gChanged;
-
- extern PrefHandle gPrefs,gOldPrefs;
-
-
- #define firstTime -1 /* the first time our hook is called, it is passed a -1 */
-
- #define reDrawList 101 /* returning 101 as item number will cause the FILE list
- to be recalculated */
-
- #define BTNON 1 /* Control value for on */
- #define BTNOFF 0 /* Control value for off */
- #define hide 1
- #define show 0
-
-
- #pragma segment Files
- pascal short MySFGetHook(MySFItem, theDialog)
- short MySFItem;
- DialogPtr theDialog;
- {
-
-
- /* MySFGetHook is a function that requires that an item number be passed */
- /* back from it. Normally, this is the same item number that was passed */
- /* to us, but not necessarily. For instance, clicks on the Quit button */
- /* get translated into clicks on the Cancel button. We could also return */
- /* values that cause the file names to be redrawn or have the whole event */
- /* ignored. However, by default, we'll return what was sent to us. */
-
- switch (MySFItem) {
- case firstTime:
- /* Before the dialog is drawn, our hook gets called with a -1. */
- /* This gives us the opportunity to change things like Button titles, etc. */
-
- gOpenPrime=true;
- gOpenClear=false;
- SetCheckBox(theDialog,iOpenPrimeButton,BTNON);
- EnableDItem(theDialog,iOpenClearButton,show);
- SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
- break;
-
- case iOpenPrimeButton:
-
- if (gOpenPrime) {
- SetCheckBox(theDialog,iOpenPrimeButton,BTNOFF);
- EnableDItem(theDialog,iOpenClearButton,hide);
- SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
- gOpenClear=false;
- gOpenPrime=!gOpenPrime;
- return(iOpenPrimeButton);
- } else {
- SetCheckBox(theDialog,iOpenPrimeButton,BTNON);
- EnableDItem(theDialog,iOpenClearButton,show);
- gOpenPrime=!gOpenPrime;
- return(iOpenPrimeButton);
- }
-
-
- break;
-
- case iOpenClearButton:
-
- if (gOpenClear) {
- SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
- gOpenClear = false;
- return(iOpenClearButton);
- }else {
- SetCheckBox(theDialog,iOpenClearButton,BTNON);
- gOpenClear = true;
- return(iOpenClearButton);
- }
- break;
-
- default:
- return(MySFItem);
- }
- }
-
-
- void DoOpen()
- {
- short thefile;
- Ptr TEText;
- int size;
- int err;
- int x;
- Str255 s;
-
- SFPGetFile(gLocation, /* location */
- "\pString", /* vestigial string */
- nil, /* fileFilter */
- 1,
- &typeList, /* array to types to show */
- MySFGetHook, /* dlgHook */
- &reply, /* record for returned values */
- rSFPGetFileDLOG, /* ID of Custom Dialog */
- nil); /* ModalDialog filterProc */
- if(reply.good){
- if(gOpenPrime){
- if(PathNameFromWD(reply.vRefNum,&s) != -1){
- PrimeFromFile(p2cstr(pStrcat(s,reply.fName)),gOpenClear);
- }else
- AlertUser(eCantPrime);
- }
- err=FSOpen(reply.fName,reply.vRefNum,&thefile);
- if(err){
- FileSystemError(reply.fName,"\popen",err,false);
- return;
- }
- err=GetEOF(thefile,&size);
- if(err){
- FileSystemError(reply.fName,"\pfind size of",err,false);
- return;
- }
- if(size>kMaxTELength){
- AlertUser(eExceedChar);
- FSClose(thefile);
- return;
- }
- TEText=NewPtr(size);
- if(TEText==nil){
- AlertUser(eNoMemory);
- FSClose(thefile);
- return;
- }
- DoNew();
- if(gText){
- err=FSRead(thefile,&size,TEText);
- if(err){
- FileSystemError(reply.fName,"\pfind size of",err,false);
- return;
- }
- TESetText(TEText,size,((DocumentPeek)gText)->docTE);
- }
- DisposPtr(TEText);
- err=FSClose(thefile);
- if(err){
- FileSystemError(reply.fName,"\pfind size of",err,false);
- return;
- }
- TESetSelect(0,0,((DocumentPeek)gText)->docTE);
- AdjustScrollbars(gText,false);
- SetWTitle(gText,reply.fName);
- gNewFile=false;
- for(x=0;x<=reply.fName[0];x++)
- gFileName[x]=reply.fName[x];
- gChanged = false;
- }
- }
-
- pascal short MySFPrimeHook(MySFItem, theDialog)
- short MySFItem;
- DialogPtr theDialog;
- {
-
-
- /* MySFPrimeHook is a function that requires that an item number be passed */
- /* back from it. Normally, this is the same item number that was passed */
- /* to us, but not necessarily. For instance, clicks on the Quit button */
- /* get translated into clicks on the Cancel button. We could also return */
- /* values that cause the file names to be redrawn or have the whole event */
- /* ignored. However, by default, we'll return what was sent to us. */
-
- switch (MySFItem) {
- case firstTime:
- /* Before the dialog is drawn, our hook gets called with a -1. */
- /* This gives us the opportunity to change things like Button titles, etc. */
-
- gOpenPrime=true;
- gOpenClear=false;
- EnableDItem(theDialog,iOpenPrimeButton,hide);
- SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
- break;
-
-
- case iOpenClearButton:
-
- if (gOpenClear) {
- SetCheckBox(theDialog,iOpenClearButton,BTNOFF);
- gOpenClear = false;
- return(iOpenClearButton);
- }else {
- SetCheckBox(theDialog,iOpenClearButton,BTNON);
- gOpenClear = true;
- return(iOpenClearButton);
- }
- break;
-
- default:
- return(MySFItem);
- }
- }
-
- void DoPrime()
- {
- Str255 s;
-
- SFPGetFile(gLocation, /* location */
- "\pString", /* vestigial string */
- nil, /* fileFilter */
- 1,
- &typeList, /* array to types to show */
- MySFPrimeHook, /* dlgHook */
- &reply, /* record for returned values */
- rSFPGetFileDLOG, /* ID of Custom Dialog */
- nil); /* ModalDialog filterProc */
- if(reply.good){
- if(PathNameFromWD(reply.vRefNum,&s) != -1){
- PrimeFromFile(p2cstr(pStrcat(s,reply.fName)),gOpenClear);
- }else
- AlertUser(eCantPrime);
- }
- DrawNewPredictions();
- }
-
- void DoSaveAs(Str255 name)
- {
- int x;
-
- SFPutFile(gLocation, /* location */
- "\pSave document as:", /* prompt string */
- name, /* original name */
- nil, /* dlgHook */
- &reply); /* record for returned values */
-
- if (reply.good){
- gNewFile=false;
- DoSave();
- for(x=0;x<=reply.fName[0];x++)
- gFileName[x]=reply.fName[x];
- SetWTitle(gText,reply.fName);
- }
- }
-
- void DoSave()
- {
- short thefile;
- int err;
- CharsHandle TEText;
- int size;
-
- if(gNewFile){
- DoSaveAs(gFileName);
- }
- err=FSOpen(reply.fName,reply.vRefNum,&thefile);
- if(err==fnfErr){
- err=Create(reply.fName,reply.vRefNum,kCreator,kFileType);
- if(err){
- FileSystemError(reply.fName,"\pcreate",err,false);
- return;
- }
- err=FSOpen(reply.fName,reply.vRefNum,&thefile);
- if(err){
- FileSystemError(reply.fName,"\popen",err,false);
- return;
- }
- }else{
- if(err){
- FileSystemError(reply.fName,"\popen",err,false);
- return;
- }
- }
- err=SetEOF(thefile,0);
- if(err){
- FileSystemError(reply.fName,"\psave",err,false);
- return;
- }
- TEText=TEGetText(((DocumentPeek)gText)->docTE);
- HLock((Handle)TEText);
- size=(long)GetHandleSize((Handle)TEText);
- err=FSWrite(thefile,&size,(Ptr)*TEText);
- if(err){
- FileSystemError(reply.fName,"\pwrite",err,false);
- return;
- }
- err=FSClose(thefile);
- if(err){
- FileSystemError(reply.fName,"\pfind size of",err,false);
- return;
- }
- gNewFile=false;
- gChanged = false;
- }
-
- void DoRevert()
- {
- short itemHit;
-
- short thefile;
- Ptr TEText;
- int size;
- int err;
-
- SetCursor(&qd.arrow);
- ParamText(gFileName, "", "", "");
-
- itemHit = Alert(rRevertAlert, nil);
-
- if(itemHit == iOk){
- err=FSOpen(reply.fName,reply.vRefNum,&thefile);
- if(err){
- FileSystemError(reply.fName,"\popen",err,false);
- return;
- }
- err=GetEOF(thefile,&size);
- if(err){
- FileSystemError(reply.fName,"\pfind size of",err,false);
- return;
- }
- if(size>kMaxTELength){
- AlertUser(eExceedChar);
- FSClose(thefile);
- return;
- }
- TEText=NewPtr(size);
- if(TEText==nil){
- AlertUser(eNoMemory);
- FSClose(thefile);
- return;
- }
- err=FSRead(thefile,&size,TEText);
- if(err){
- FileSystemError(reply.fName,"\pfind size of",err,false);
- return;
- }
- TESetText(TEText,size,((DocumentPeek)gText)->docTE);
- DisposPtr(TEText);
- err=FSClose(thefile);
- if(err){
- FileSystemError(reply.fName,"\pclose",err,false);
- return;
- }
- TESetSelect(0,0,((DocumentPeek)gText)->docTE);
- AdjustScrollbars(gText,false);
- DrawWindow(gText);
- DrawNewPredictions();
- }
- }
-
- pascal short MySFNoneHook(MySFItem, theDialog)
- short MySFItem;
- DialogPtr theDialog;
- {
- #pragma unused (theDialog)
- switch (MySFItem) {
- case firstTime:
- /* Before the dialog is drawn, our hook gets called with a -1. */
- /* This gives us the opportunity to change things like Button titles, etc. */
- gNone=false;
- break;
-
-
- case iNoneButton:
- gNone=true;
- return(iCancel);
- break;
-
- default:
- return(MySFItem);
- }
- }
-
-
- GetPrimeFile(Boolean PrimeOrZero, PrefHandle newPrefs)
- {
-
- SFPGetFile(gLocation, /* location */
- "\pString", /* vestigial string */
- nil, /* fileFilter */
- 1,
- &typeList, /* array to types to show */
- MySFNoneHook, /* dlgHook */
- &reply, /* record for returned values */
- rGetFileNoneDLOG, /* ID of Custom Dialog */
- nil); /* ModalDialog filterProc */
- if(reply.good){
- if(PrimeOrZero){
- (*newPrefs)->pHasPrime=true;
- pStrcpy((*newPrefs)->pPrimeName,reply.fName);
- (*newPrefs)->pPrimeDirID=GetDirID(reply.vRefNum);
- (*newPrefs)->pPrimeRefNum=GetRealRefNum(reply.vRefNum);
- } else {
- (*newPrefs)->pHasZero=true;
- pStrcpy((*newPrefs)->pZeroName,reply.fName);
- (*newPrefs)->pZeroDirID=GetDirID(reply.vRefNum);
- (*newPrefs)->pZeroRefNum=GetRealRefNum(reply.vRefNum);
- }
- return(true);
- }else{
- if(gNone) {
- if(PrimeOrZero){
- (*newPrefs)->pHasPrime=false;
- } else {
- (*newPrefs)->pHasZero=false;
- }
- return(true);
- }
- return(false);
- }
- }
-